package com.ilop.sthome.ui.activity.config;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.util.Log;
import android.view.View;

import com.ilop.sthome.ui.activity.main.ContainLoginActivity;
import com.example.common.utils.LiveDataBus;
import com.example.common.utils.MediaPlayerUtil;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.enums.ProductGroup;
import com.ilop.sthome.data.enums.SmartDevice;
import com.ilop.sthome.data.event.EventAnswerOK;
import com.ilop.sthome.data.event.EventRefreshDevice;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.mvp.contract.device.AddDeviceContract;
import com.ilop.sthome.mvp.present.device.AddDevicePresenter;
import com.ilop.sthome.ui.activity.device.GatewayMainActivity;
import com.ilop.sthome.ui.activity.main.MainActivity;
import com.ilop.sthome.utils.CoderUtils;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivityAddDeviceGuideBinding;

import java.util.Objects;

/**
 * @author skygge
 * @date 2019-10-28.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：添加子设备(成功或失败)页面
 */

public class AddDeviceGuideActivity extends ContainLoginActivity<AddDevicePresenter, ActivityAddDeviceGuideBinding> implements AddDeviceContract.IView {

    private String mDeviceType;
    private String mDeviceName;
    private String mNickName;
    private String mBindRoom;
    private int mDeviceId;
    private boolean mSingle = true;
    private boolean isSubDevice;
    private MediaPlayerUtil mMediaPlayer;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_device_guide;
    }

    @Override
    protected void initialize() {
        super.initialize();
        mDeviceId = getIntent().getIntExtra(CommonId.KEY_DEVICE_ID,0);
        mDeviceName = getIntent().getStringExtra(CommonId.KEY_DEVICE_NAME);
        mDeviceType = getIntent().getStringExtra(CommonId.KEY_DEVICE_TYPE);
        mNickName = getIntent().getStringExtra(CommonId.KEY_DEVICE);
        mBindRoom = getIntent().getStringExtra(CommonId.KEY_BIND_ROOM);
        isSubDevice = getIntent().getBooleanExtra(CommonId.KEY_SUB_DEVICE, false);
    }

    @Override
    protected void initPresent() {
        super.initPresent();
        mPresent = new AddDevicePresenter(mContext, mDeviceName, mNickName);
    }

    @Override
    protected void initView() {
        super.initView();
        mMediaPlayer = new MediaPlayerUtil(mContext);
    }

    @Override
    protected void initData() {
        super.initData();
        ProductGroup mConfig = ProductGroup.getType(mDeviceType);
        if(mDeviceId==0){
            mDBind.ivConfigGuide.setBackgroundResource(SmartDevice.getType(mDeviceType).getDrawGuideResId());
            mDBind.tvAddSubTitle.setText(getString(Objects.requireNonNull(mConfig).getTypeStrId()));
            if(mConfig == ProductGroup.EE_DEV_LOCK){
                showGuideText(getString(R.string.ali_add_guide_lock_1), getString(R.string.ali_add_guide_lock_2));
            }else if(mConfig== ProductGroup.EE_DEV_LIGHTING_MODULE){
                showGuideText(getString(R.string.ali_add_switch_1), getString(R.string.ali_add_switch_2));
            }else if(mConfig == ProductGroup.EE_TEMP_OUTDOOR_SIREN){
                showGuideText(getString(R.string.ali_add_shiwai_1), getString(R.string.ali_add_shiwai_2));
            }else if(mConfig == ProductGroup.EE_DEV_WT_ALARM){
                showGuideText(getString(R.string.touch_two_metal_points_three_times), getString(R.string.ali_add_shiwai_2));
            }else {
                showGuideText(getString(R.string.add_device_hint), getString(R.string.ali_add_shiwai_2));
            }
        }else {
            DeviceInfoBean deviceInfoBean = DeviceDaoUtil.getInstance().findByDeviceId(mDeviceName, mDeviceId);
            mDBind.ivConfigGuide.setBackgroundResource(SmartDevice.getType(deviceInfoBean.getDevice_type()).getDrawGuideResId());
            mDBind.tvAddSubTitle.setText(getString(R.string.replace_equipment));
            mPresent.onReplaceDevice(mDeviceId, mDeviceType);
        }
        if (mConfig == ProductGroup.EE_DEV_PIR){
            mDBind.guideText0.setText(getString(R.string.pir_start_hint));
        }else {
            mDBind.guideText0.setVisibility(View.INVISIBLE);
        }
        AnimationDrawable mAnimation = (AnimationDrawable) mDBind.ivConfigGuide.getBackground();
        mAnimation.start();
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.ivSubBack.setOnClickListener(view -> {
            mPresent.onCancel();
            finish();
        });
    }

    @Override
    protected void initDataBus() {
        super.initDataBus();
        LiveDataBus.get().with(CommonId.KEY_EVENT_REFRESH_DEVICE, EventRefreshDevice.class).observe(this, event -> {
            if (event!=null){
                Log.i(TAG, "onEventMainThread: subDevice " +  event.getDevice_status());
                if (mDeviceName.equals(event.getDeviceName()) && event.getDevice_id() > 0 && mDeviceId == 0) {
                    if (event.getType() == 0 && mSingle) {
                        showAddSuccess();
                        mPresent.onAddNewDevice(event.getDevice_id(), event.getDevice_type(), mBindRoom);
                        mSingle = false;
                    } else if (event.getType() == 1 && mSingle) {
                        showToast(getString(R.string.device_has_been_exist));
                        mSingle = false;
                        mPresent.onSkipToDetail(event.getDevice_type(), event.getDevice_id());
                    }
                } else {
                    if (mDeviceName.equals(event.getDeviceName()) && mDeviceId == event.getDevice_id()) {
                        showToast(getString(R.string.replace_success));
                        finish();
                    }
                }
            }
        });

        LiveDataBus.get().with(CommonId.KEY_ANSWER_OK, EventAnswerOK.class).observe(this, event -> {
            if (event!=null){
                int result = CoderUtils.getAnswerResult(event.getData_str1(), event.getData_str2());
                Log.i(TAG, "initDataBus: answerOK--" + result);
                if (result == 5) mPresent.onModifySuccess();
            }
        });
    }

    @Override
    public void showGuideText(String text1, String text2) {
        mDBind.guideText1.setText(text1);
        mDBind.guideText2.setText(text2);
    }

    @Override
    public void showAddSuccess() {
        showToast(getString(R.string.add_success));
        mMediaPlayer.playSuccess();
    }

    @Override
    public void showToastMsg(String msg) {
        showToast(msg);
    }

    @Override
    public void skipActivity(Intent intent) {
        if (isSubDevice){
            intent.putExtra(CommonId.KEY_DEVICE_TYPE, 0);
        }else {
            intent.putExtra(CommonId.KEY_DEVICE_TYPE, 1);
        }
        startActivity(intent);
        finish();
    }

    @Override
    public void finishActivity() {
        Intent intent = new Intent();
        if (isSubDevice){
            intent.setClass(mContext, GatewayMainActivity.class);
        }else {
            intent.setClass(mContext, MainActivity.class);
        }
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);
    }

    @Override
    public void onBackPressed() {
        mPresent.onCancel();
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mMediaPlayer!=null) {
            mMediaPlayer.destroy();
        }
    }

}
